package de.sp.games.snake12k;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class HighscoreSate extends BasicGameState {
	private int stateID = 4;
	private Image backgroundImage;
	private Image HighImage;
	private UnicodeFont uFBigger;

	String[] highscore_data = new String[5];

	public HighscoreSate(int stateID) {
		this.stateID = stateID;

	}

	public int getID() {
		return stateID;
	}

	@SuppressWarnings("unchecked")
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		backgroundImage = new Image("ressource/box_empty.jpg");
		backgroundImage.setFilter(Image.FILTER_NEAREST);
		HighImage = new Image("ressource/highscores.png");
		HighImage.setFilter(Image.FILTER_NEAREST);
		uFBigger = new UnicodeFont(new java.awt.Font("Verdana", java.awt.Font.BOLD, 45));
		uFBigger.getEffects().add(new ColorEffect(java.awt.Color.YELLOW));
		uFBigger.addAsciiGlyphs();
		uFBigger.loadGlyphs();
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input input = gc.getInput();

		if (input.isKeyPressed(Input.KEY_ESCAPE)) {
			sbg.enterState(Snake12k.TITLEMENUSTATE);
		}

		if (SUtil.needHighScoreUpdate) {
			SUtil.needHighScoreUpdate = false;
			ScoreList scoreList = ScoreList.getInstance();
			System.out.println("HighScore updated!");
			for (int i = 0; i < highscore_data.length; i++) {
				highscore_data[i] = i + 1 + ". " + scoreList.getScore(i).getName() + " - " + String.valueOf(scoreList.getScore(i).getScoreNumber());
			}
		}
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.scale((1.0f / 16 * gc.getWidth()), (1.0f / 16) * (gc.getHeight() - 30));
		backgroundImage.draw();
		g.resetTransform();

		HighImage.draw((gc.getWidth() / 2) - (HighImage.getWidth() / 2), 35);

		for (int i = 0; i < highscore_data.length; i++) {
			String tmp = highscore_data[i];
			uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(tmp) / 2), 175 + i * 80, tmp);
		}

		g.drawString("Ver: " + Snake12k.version + " 'ESC' for going back", 10, gc.getHeight() - 25);
	}
}
